package com.cqgcxy.shopping.controller;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.CommException;
import com.cqgcxy.shopping.domain.User;
import com.cqgcxy.shopping.service.UserService;





@WebServlet(urlPatterns={"/user/register.do"})
public class RegisterServlet extends HttpServlet{
	private static final long serialVersionUID = -8496579934036520223L;
	private Logger logger=LoggerFactory.getLogger(getClass());
	private UserService userService=new UserService();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);

	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.info("shoppingRegister.dopost");
		//1.接手前台传递过来的参数
				try {
					logger.debug("执行第一步");		
				String usercode =req.getParameter("usercode");//账号
				String password=req.getParameter("password");//密码
				String phonenumber=req.getParameter("phonenumber");//账号手机
				String kaptcha=req.getParameter("kaptcha");//验证码
				logger.debug("获取参数成功：{},{},{},{}",usercode,password,phonenumber,kaptcha);
				//2.参数类型转换
				logger.debug("类型转换成功");
				//3.合法性检查
				
				logger.debug("执行第二步");
				
				if(usercode==null||usercode.trim().length()<=0) {
					throw new CommException("用户名不能为空");
				}

				if(password==null||password.trim().length()<=0) {
					throw new CommException("密码不能为空");
				}
				
				if(phonenumber==null||phonenumber.trim().length()<=0) {
					throw new CommException("账号不能为空");
				}

				if(kaptcha==null||kaptcha.trim().length()<0) {
					throw new CommException("验证码错误");
				}
				
				HttpSession session2=req.getSession();
				Object object=session2.getAttribute("kaptcha"); //获取session中的验证码
				
				if(!kaptcha.equals(object)) {
					throw new CommException("验证码输入错误");
				}
				logger.debug("校验成功");
				//两次输入的密码不相等
				logger.debug("执行第三步");
				//4.参数封装为对象
				User user=new User();
				user.setUsercode(usercode);
				user.setPhonenumber(phonenumber);
				user.setPassword(password);		
				//5.调用业务方法
				userService.register(user);
				logger.debug("执行第四步");
				//6.1转发或重定向（可传递数据给页面）
				//req.getRequestDispatcher("/index.jsp").forward(req, resp);//转发,把参数转走，上下文没有改变
				resp.sendRedirect(req.getContextPath()+"/login.jsp");//重定向，上下文可能改变,req.getContextPath()取得当前的上下文
				logger.info("注册成功");
			}catch(Exception e){
				//6.2转发或重定向（可传递错误信息给页面）
				req.setAttribute("error", e.getMessage());
				resp.sendRedirect(req.getContextPath()+"/register.jsp");
				logger.info("注册失败：()",e.getMessage());
			}
		
	}
	
	
	
	
}
